package com.xinzhi.savingpot.task.service.impl;

import com.xinzhi.savingpot.task.entity.TaskClassInfo;
import com.xinzhi.savingpot.task.mapper.TaskClassInfoMapper;
import com.xinzhi.savingpot.task.vo.TaskCLassVo;
import com.xinzhi.savingpot.task.service.ITaskClassInfoService;
import com.xinzhi.savingpot.util.BaseResponse;
import com.xinzhi.savingpot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-01-05
 */
@Service
public class TaskClassInfoServiceImpl implements ITaskClassInfoService {

    @Autowired
    private TaskClassInfoMapper classMapper;
    @Autowired
    private RedisUtil redisUtil;

    //TODO 这是一个任务模块任务类型的查询
    @Override
    public BaseResponse queryClassList() {
        BaseResponse baseResponse;
        if (!redisUtil.hasKey("classList")) {
            List<TaskClassInfo> classList = classMapper.queryAllType();
            if (!classList.isEmpty()) {
                List<TaskCLassVo> tcvs = new ArrayList<>();
                TaskCLassVo tcv;
                for (TaskClassInfo tci: classList) {
                    tcv = new TaskCLassVo();
                    tcv.setTaskClassId(tci.getTaskClassId());
                    tcv.setTaskClassName(tci.getTaskClassName());
                    tcvs.add(tcv);
                }
                baseResponse = new BaseResponse(1, "获取成功", tcvs);
                redisUtil.set("classList",tcvs,60*60*24);
                return baseResponse;
            }
        }
        List<TaskCLassVo> tcvs = (List<TaskCLassVo>) redisUtil.get("classList");
        baseResponse = new BaseResponse(1, "获取成功", tcvs);
        return baseResponse;
    }
}
